/*
 * 轮播图
 */
function Slide(elem,ms,d,ind){
	this.el = elem;
	this.w=parseInt(getStyle(elem,"width"));
	this.h=parseInt(getStyle(elem,"width"));
	
	var a = this.el.children;//获取传进来的子节点
	this.ul = a[0];//获取的其实就是ul，第一个孩子
	this.ol = a[1];//获取的其实就是ul，第二个孩子
	
	
	this.lBtn = a[2];//左右点击
	this.rBtn = a[3];
	
	
	this.olspan = this.ol.getElementsByTagName("span");//获取下面圆点的所有元素
	this.l = this.olspan.length;//获取元素的总长度
	this.ul.style.width = this.w*(this.l+1)+"px";	// ul的宽度
	/*this.lBtn.style.top = this.rBtn.style.top = (this.h-this.rBtn.offsetHeight)/2+1000+"px";*///居中
	this.ms = ms;	// 每隔多久执行一次滚动
	this.d = d;	// 轮播时方向
	
	this.ul.innerHTML += this.ul.children[0].outerHTML;	//将第一张图片复制到最后一个位置上
	
	var that = this;//方便调用
	
	this.now = ind;//传进来下标
	that.prev = -that.now*that.w;//就是下标乘以每个图片的宽度加上负号就变成上一个的意思
	
	
	/***************************************/
	
	this.run = function(){//添加了一个方法
		var i=0, l=that.l, btns=that.olspan, btn;//将上面的获取的所有span元素长度赋给l
		for( ; i<l; i++){
			btn = btns[i];//第i个圆点span
			btn.i = i;//自定义属性下标
			btn.onclick = function(){
				that.now = this.i;
				that.tab();//调用tab（）方法
			}
		}
		
		that.timer = setInterval(that.next, that.ms);
		that.el.onmouseover = that.over;
		that.el.onmouseout = that.out;
		that.lBtn.onclick = function(){
			that.now--;
			that.d = -1;
			that.tab();
		}
		that.rBtn.onclick = function(){
			that.now++;
			that.d = 1;
			that.tab();
		}
		that.lBtn.onmousedown = that.rBtn.onmousedown = function(){
			return false;
		}
	}
	
	
	/***************************************/
	
	this.tab = function(){
		that.ul.style.left = that.prev+"px";	// 每次运动时，先瞬间定位到上一次的目标值，然后再执行本次运动
		
		if( that.now == that.l ){
			that.prev = 0;
			startMove(that.ul, {"left":-that.now*that.w}, function(){
				that.ul.style.left = "0px";
			});
			that.now = 0;
		}else if( that.now == -1 ){
			that.now = that.l-1;
			that.ul.style.left = -that.l*that.w+"px";
			that.prev = -that.now*that.w;
			startMove(that.ul, {"left":that.prev});
		}else{
			that.prev = -that.now*that.w;
			startMove(that.ul, {"left":that.prev});
		}
		
		// 样式
		for( var i=0,l=that.l; i<l; i++ ){
			that.olspan[i].className = "";
		}
		that.olspan[that.now].className = "selected";
	}
	
	this.next = function(){
		that.now += that.d;
		that.tab();
	}
	
	this.over = function(){
		clearInterval(that.timer);
		startMove(that.lBtn, {"opacity":100});
		startMove(that.rBtn, {"opacity":100});
	}
	this.out = function(){
		that.timer = setInterval(that.next, that.ms);
		startMove(that.lBtn, {"opacity":0});
		startMove(that.rBtn, {"opacity":0});
	}
	
	this.tab();
	this.run();
}


